package net.qhzw.vip.qiyu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import net.qhzw.vip.qiyu.entity.HotSearch;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.HotSearchService;
import net.qhzw.vip.qiyu.util.PageParam;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/hotSearch")
public class HotSearchController {

    @Autowired
    private HotSearchService hotSearchService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public JsonResult<IPage<HotSearch>> page(HotSearch hotSearch, PageParam<HotSearch> page){
        IPage<HotSearch> hotSearchs = hotSearchService.page(page,new LambdaQueryWrapper<HotSearch>()
                .like(StrUtil.isNotBlank(hotSearch.getContent()), HotSearch::getContent,hotSearch.getContent())
                .like(StrUtil.isNotBlank(hotSearch.getTitle()), HotSearch::getTitle,hotSearch.getTitle())
                .eq(hotSearch.getStatus()!=null, HotSearch::getStatus,hotSearch.getStatus())
        );
        return JsonResult.ok(hotSearchs);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public JsonResult info(@PathVariable("id") Long id){
        HotSearch hotSearch = hotSearchService.getById(id);
        return JsonResult.ok(hotSearch);
    }

    /**
     * 保存
     */
    @PostMapping
    public JsonResult save(@RequestBody @Valid HotSearch hotSearch){
        hotSearch.setRecDate(new Date());
        hotSearch.setShopId(SecurityUtils.getLoginUser().getShopId());
        hotSearchService.save(hotSearch);
        //清除缓存
        hotSearchService.removeHotSearchDtoCacheByshopId(SecurityUtils.getLoginUser().getShopId());
        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    public JsonResult update(@RequestBody @Valid HotSearch hotSearch){
        hotSearchService.updateById(hotSearch);
        //清除缓存
        hotSearchService.removeHotSearchDtoCacheByshopId(SecurityUtils.getLoginUser().getShopId());
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public JsonResult delete(@RequestBody List<Long> ids){
        hotSearchService.removeByIds(ids);
        //清除缓存
        hotSearchService.removeHotSearchDtoCacheByshopId(SecurityUtils.getLoginUser().getShopId());
        return JsonResult.ok();
    }
}

